package com.laf.manage.controller.admin;

import com.laf.common.annotation.Log;
import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.AjaxResult;
import com.laf.common.core.page.TableDataInfo;
import com.laf.common.core.text.Convert;
import com.laf.common.enums.BusinessType;
import com.laf.common.utils.ServletUtils;
import com.laf.manage.domain.dto.RollbackDTO;
import com.laf.manage.domain.po.Dynamic;
import com.laf.manage.domain.query.DynamicQuery;
import com.laf.manage.domain.vo.DynamicVO;
import com.laf.manage.enums.VerifyStatusEnum;
import com.laf.manage.service.IDynamicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

import static com.laf.common.core.page.TableSupport.PAGE_NUM;
import static com.laf.common.core.page.TableSupport.PAGE_SIZE;

/**
 * 内容管理Controller
 *
 * @author zian
 * @date 2024-11-09
 */
@Slf4j
@Api(tags = "内容管理管理")
@RestController("adminDynamicController")
@RequestMapping("/admin/dynamic")
@RequiredArgsConstructor
public class DynamicController extends BaseController {
    @Autowired
    private IDynamicService dynamicService;

    /**
     * 通过审核
     */
    @ApiOperation("通过审核")
    @PreAuthorize("@ss.hasPermi('manage:content:edit')")
    @PutMapping("/pass/{id}")
    public AjaxResult passContentById(@PathVariable Long id) {
        log.info("通过审核 参数有：{}", id);
        dynamicService.updateStatus(id, VerifyStatusEnum.VERIFY_PASS, null);
        return AjaxResult.success();
    }

    /**
     * 未通过审核
     */
    @ApiOperation("未通过审核")
    @PreAuthorize("@ss.hasPermi('manage:content:edit')")
    @PutMapping("/reject")
    public AjaxResult rejectContentById(@RequestBody @Validated RollbackDTO  dto) {
        log.info("未通过审核 参数有：{}", dto);
        dynamicService.updateStatus(dto.getId(), VerifyStatusEnum.VERIFY_NOT_PASS, dto.getRemark());
        return AjaxResult.success();
    }

    /**
     * 查询内容管理列表
     */
    @ApiOperation("查询内容管理列表")
    @PreAuthorize("@ss.hasPermi('manage:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(DynamicQuery query) {
        log.info("查询失物管理列表 参数有：{}", query);
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return dynamicService.selectDynamicDocList(query, pageNum, pageSize);
    }


    /**
     * 获取内容管理详细信息
     */
    @ApiOperation("获取内容管理详细信息")
    @PreAuthorize("@ss.hasPermi('manage:content:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        DynamicVO vo = dynamicService.getDetailsById(id , true);
        return success(vo);
    }

    /**
     * 新增内容管理
     */
    @ApiOperation("新增内容管理")
    @PreAuthorize("@ss.hasPermi('manage:content:add')")
    @Log(title = "内容管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dynamic dynamic) {
        return toAjax(dynamicService.save(dynamic));
    }

    /**
     * 修改内容管理
     */
    @ApiOperation("修改内容管理")
    @PreAuthorize("@ss.hasPermi('manage:content:edit')")
    @Log(title = "内容管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dynamic dynamic) {
        return toAjax(dynamicService.updateById(dynamic));
    }

    /**
     * 删除内容管理
     */
    @ApiOperation("删除内容管理")
    @PreAuthorize("@ss.hasPermi('manage:content:remove')")
    @Log(title = "内容管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        dynamicService.delByIds(Arrays.asList(ids));
        return success();
    }
}
